package com.liangtian.mdm.dm.common.enums;

import org.apache.commons.lang3.ObjectUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 2016-08-11
 *
 * @author honye
 */
public enum Flag {

    //审核状态
    STATE_0("0", "state", "新增"),
    STATE_1("1", "state", "校验无错误"),
    STATE_2("2", "state", "校验有错误"),
    STATE_3("3", "state", "审核拒绝"),
    STATE_4("4", "state", "审核通过"),
    STATE_5("5", "state", "审核中"),

    //数据状态
    STATUS_0("0", "status", "活动"),
    STATUS_1("1", "status", "冻结"),

    //分发状态
    DISPATCH_0("0", "dis_flag", "待分发"),
    DISPATCH_1("1", "dis_flag", "分发中"),
    DISPATCH_2("2", "dis_flag", "分发成功"),
    DISPATCH_3("3", "dis_flag", "分发失败"),

    //操作状态
    OPER_0("submit", "opertype", "提交"),
    OPER_1("update", "opertype", "变更"),
    OPER_2("approve", "opertype", "审核"),
    OPER_3("freeze", "opertype", "冻结"),
    OPER_4("active", "opertype", "解冻"),
    OPER_5("dispatch", "opertype", "分发"),
    OPER_6("add", "opertype", "新增"),
    OPER_99("", "opertype", "默认"),
    OPER_98("updateHis", "opertype", "变更历史"),
    OPER_100("apply", "opertype", "list"),

    //查询附加标识
    FORQUERY("1", "plus", "弹出选择查询");

    private String code;
    private String column;
    private String desc;

    private Flag(String code, String column, String desc) {
        this.code = code;
        this.column = column;
        this.desc = desc;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getColumn() {
        return column;
    }

    public void setColumn(String column) {
        this.column = column;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    /**
     * 返回给定属性给定值的对应描述
     *
     * @param value  标识值
     * @param column 标识名(列名/属性名)
     * @return
     */
    public static Object toDesc(Object value, String column) {

        //	for(Flag f : Flag.values()){
        //		if(f.getCode().equals(code) && f.getColumn().equalsIgnoreCase(column))
        //			return f.getDesc();
        //	}

        column = column.toLowerCase();
        if (flagMap.containsKey(column)) {
            List<Flag> flags = flagMap.get(column);
            for (Flag f : flags) {
                if (f.getCode().equals(ObjectUtils.isEmpty(value))) //String.valueOf(value)))
                    return f.getDesc();
            }
        }
        return value;
    }

    /**
     * 返回给定属性描述的对应值
     *
     * @param value  描述
     * @param column 标识名(列名/属性名)
     * @return
     */
    public static Object toValue(Object value, String column) {
        column = column.toLowerCase();
        if (flagMap.containsKey(column)) {
            List<Flag> flags = flagMap.get(column);
            for (Flag f : flags) {
                if (f.getDesc().equals(String.valueOf(value)))
                    return f.getCode();
            }
        }
        return value;
    }

    public List<Flag> getFlagsByColumn(String column) {

        List<Flag> list = new ArrayList<Flag>();
        for (Flag f : Flag.values()) {
            if (f.getColumn().equalsIgnoreCase(column))
                list.add(f);
        }
        return list;
    }

    /**
     * 静态化，减少搜索判断
     */
    @SuppressWarnings("serial")
    public static Map<String, List<Flag>> flagMap = new HashMap<String, List<Flag>>() {
        {
            //数据状态
            List<Flag> status = new ArrayList<Flag>() {
                {
                    add(STATUS_0);
                    add(STATUS_1);
                }
            };
            put("status", status);

            //审核状态
            List<Flag> state = new ArrayList<Flag>() {
                {
                    add(STATE_0);
                    add(STATE_1);
                    add(STATE_2);
                    add(STATE_3);
                    add(STATE_4);
                    add(STATE_5);
                }
            };
            put("state", state);

            //分发状态
            List<Flag> dispatch = new ArrayList<Flag>() {
                {
                    add(DISPATCH_0);
                    add(DISPATCH_1);
                    add(DISPATCH_2);
                    add(DISPATCH_3);
                }
            };
            put("dispatch", dispatch);

            //操作状态
            List<Flag> opertype = new ArrayList<Flag>() {
                {
                    add(OPER_0);
                    add(OPER_1);
                    add(OPER_2);
                    add(OPER_3);
                    add(OPER_4);
                    add(OPER_6);
                    add(OPER_99);
                }
            };
            put("opertype", opertype);
        }
    };

}
